<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>One To One - Shared Primary Key</title>
</head>
<body>
  <pre class="brush: java">
//User.java source
@Entity(name="OtoSharedPkUser") 
@Table(name = "oto_sharedpk_user")
@NamedQuery(name = "getProfileForUser", 
	query = "SELECT p FROM OtoSharedPkProfile p WHERE p.id = :userId")
public class User implements Serializable {
	
	private int id;
	private String name;
	
	public User() {}

	public User(String name) {
		this.name = name;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	
	private void setId(int id) {
		this.id = id;
	}
	
	@Column(nullable = false, length = 30)
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}

///////////////////////////////////////////////////

//Profile.java source
@Entity(name = "OtoSharedPkProfile") 
@Table(name = "oto_sharedpk_profile")
public class Profile implements Serializable {

	private int id;
	private String email;
	private User user;
	
	public Profile() {}
	
	public Profile(String email) {
		this.email = email;
	}
	
	@Id
	public int getId() {
		return id;
	}

	private void setId(int id) {
		this.id = id;
	}

	@Column(nullable = false, length = 30)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@MapsId 
	@OneToOne
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
  </pre>
</body>
</html>
